import { forkJoin } from 'rxjs';
import { LvConfig } from '@iux/live';
import { I18NService } from '../services';
import { ICONS_CONFIG } from '../config';

export class Initializer {
  /**
   * 初始化组件全局配置
   * 初始化icon配置
   * @param i18n 国际化
   */
  static load(i18n: I18NService) {
    Initializer.lvConfig(i18n);
    Initializer.iconConfig();

    return () => Initializer.I18NConfig(i18n);
  }

  // I18N服务配置
  static I18NConfig(i18n: I18NService) {
    return forkJoin(i18n.load(`assets/i18n/${i18n.language}/common.json`)).toPromise();
  }

  // icon配置
  static iconConfig(): void {
    document.body.insertAdjacentHTML('afterbegin', ICONS_CONFIG);
  }

  // 组件配置
  static lvConfig(i18n: I18NService): void {
    LvConfig.language = i18n.language as any;
    LvConfig.dataTableOptions = {
      lvResizeMinWidth: 96,
    };
    LvConfig.messageOptions = {
      lvShowCloseButton: true,
      lvPosition: 'topRight',
      lvMaxStacks: 5,
    };
  }
}
